#!/bin/bash

# Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


set -e

pushd ${1}
echo "[drivers] peripheral nearlink sle library copy path: [$1], [$2], [$3], [$4], [$5], [$6], [$7]"
ROOT_BUILD_DIR=${2}

export BSP_MODULES_OUT=${ROOT_BUILD_DIR}/modules

rm -rf ${BSP_MODULES_OUT}

mkdir -p ${BSP_MODULES_OUT}
mkdir -p ${BSP_MODULES_OUT}/sle/output/bin
mkdir -p ${BSP_MODULES_OUT}/sle/firmware

############  copy sle  library ##################
cp -rvf sle/prebuilts_lib/output/bin/* ${BSP_MODULES_OUT}/sle/output/bin/ && cp -rvf sle/prebuilts_lib/firmware/* ${BSP_MODULES_OUT}/sle/firmware/

export KERNELBUILD_PATH=${4}
KERNEL_VERSION=${7}
export KERNELPATH=${KERNELBUILD_PATH}/OBJ/${KERNEL_VERSION}
SIGN_FILE="${KERNELPATH}/scripts/sign-file"
SIGN_METHOD="sha512"
CERT_FILE="${KERNELPATH}/certs/signing_key.pem ${KERNELPATH}/certs/signing_key.x509"

[ -f ${SIGN_FILE} ] && {
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/plat_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/sle_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/wifi_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/ble_soc.ko
    ${SIGN_FILE} ${SIGN_METHOD} ${CERT_FILE} ${BSP_MODULES_OUT}/sle/output/bin/bp_test.ko
}
